import lib.database as database
import threading
import time


def add_csv(n):
    db = database.database()

    dep_time = '2017-06-' + str(n + 1) + '-%'
    start_tiem = time.time()
    fetch_data = db.search_dep(dep_time)
    end_time = time.time()
    print(str(n) + '-- Finish search_dep: timing-- ', (end_time - start_tiem))
    print(str(n) + '-- begin writing data')
    mess = ''
    col_head = db.get_col_name('dwv_haikou')
    for index in range(len(col_head) - 1):
        mess += str(col_head[index]) + ','
    mess += col_head[-1] + '\n'
    print(mess)

    for item in fetch_data:
        for index in range(len(item) - 1):
            mess += str(item[index]) + ','
        mess += item[-1] + '\n'

    print(mess)
    with open('./csv/' + dep_time + '.csv', "w") as f:
        f.write(mess)


if __name__ == '__main__':

    for i in range(9, 18):
        threading.Thread(target=add_csv, args=(i,)).start()
